//
//  YTDriverAuthBasicViewModel.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/5/12.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTDriverAuthBasicViewModel.h"
#import "YTUserProfileServices.h"

@interface YTDriverAuthBasicViewModel ()

@property (copy, readwrite, nonatomic) NSString *realName;
@property (copy, readwrite, nonatomic) NSString *male;
@property (copy, readwrite, nonatomic) NSString *birthDay;
@property (copy, readwrite, nonatomic) NSString *idType;
@property (copy, readwrite, nonatomic) NSString *idNum;
@property (copy, nonatomic) NSString *liveCity;
@property (copy, nonatomic) NSString *service;

@property (copy, nonatomic) NSString *liveCityID;
@property (copy, nonatomic) NSString *serviceID;

@property (strong, readwrite, nonatomic) UIImage *faceImage;
@property (strong, readwrite, nonatomic) UIImage *backImage;

@property (strong, readwrite, nonatomic) RACCommand *nextCommand;
@property (strong, readwrite, nonatomic) RACCommand *liveCommand;
@property (strong, readwrite, nonatomic) RACCommand *serviceCommand;
@end

@implementation YTDriverAuthBasicViewModel

- (void)initialize {
    
    id (^inputed)(NSString *input) = ^id (NSString *input) {
        return @(input.length > 1);
    };
    id (^selected)(NSString *text) = ^id (NSString *text) {
        return @(![text isEqualToString:@"请选择"]);
    };
    id (^image)(UIImage *image) = ^id (UIImage *image) {
        return @(image != nil);
    };

    RACSignal *realNameSignal = [RACObserve(self, realName) map:inputed];
    RACSignal *birthdaySignal = [RACObserve(self, birthDay) map:selected];
    RACSignal *serviceSignal = [RACObserve(self, serviceID) map:selected];
    RACSignal *liveSignal = [RACObserve(self, liveCityID) map:selected];
    RACSignal *idSignal = [RACObserve(self, idType) map:selected];
    RACSignal *idNumSignal = [RACObserve(self, idNum) map:inputed];
    RACSignal *faceSignal = [RACObserve(self, faceImage) map:image];
    RACSignal *backSignal = [RACObserve(self, backImage) map:image];
    
    _liveCityTerminal = RACChannelTo(self, liveCity);
    _serviceCityTerminal = RACChannelTo(self, service);
    
    RACSignal *enableSignal = [RACSignal combineLatest:@[realNameSignal, birthdaySignal, serviceSignal, liveSignal, idSignal, idNumSignal, faceSignal, backSignal]
                                                reduce:^id(NSNumber *realName, NSNumber *male, NSNumber *service, NSNumber *live,
                                                           NSNumber *type, NSNumber *num, NSNumber *face, NSNumber *back){
        return @(realName.boolValue && male.boolValue && service.boolValue && live.boolValue && type.boolValue && num.boolValue && face.boolValue && back.boolValue);
    }];
    _nextCommand = [[RACCommand alloc] initWithEnabled:enableSignal signalBlock:^RACSignal *(id input) {
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        dict[@"name"] = self.realName;
        dict[@"idCard"] = self.idType;
        dict[@"cityId"] = self.liveCityID;
        dict[@"female"] = self.male;
        dict[@"serviceCity"] = self.serviceID;
        dict[@"cardTypeId"] = self.idNum;
        NSString *faceString = [UIImage yt_imageToString:[[UIImage alloc] yt_imageWithImageSimple:self.faceImage scaledToSize:CGSizeMake(600, 300)]];
        dict[@"idCardImage"] = faceString;
        NSString *backString = [UIImage yt_imageToString:[[UIImage alloc] yt_imageWithImageSimple:self.backImage scaledToSize:CGSizeMake(600, 300)]];
        dict[@"idCardImage_back"] = backString;
        dict[@"birthday"] = self.birthDay;
        return [YTUserProfileServices driverAuthStep1WithData:dict];
    }];
    
    [self citySelect];
}

#pragma mark - 城市选择

- (void)citySelect {
    self.liveCity = @"请选择";
    self.service = @"请选择";
    
    @weakify(self);
    _liveCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(RACTuple *input) {
        @strongify(self);
        NSArray<NSString *> *cityArray = input.first;
        NSArray<NSString *> *cityIDArray = input.second;
        self.liveCityID = cityIDArray[0];
        self.liveCity = cityArray[0];
        return [RACSignal empty];
    }];
    
    _serviceCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(RACTuple *input) {
        NSArray<NSString *> *cityArray = input.first;
        NSArray<NSString *> *cityIDArray = input.second;
        NSMutableString *text = [[NSMutableString alloc] init];
        NSMutableString *idText = [[NSMutableString alloc] init];
        
        for (NSInteger i = 0; i < cityArray.count; i++) {
            [text appendFormat:@" %@", cityArray[i]];
            if (i == 0) {
                [idText appendString:cityIDArray[i]];
            } else {
                [idText appendFormat:@",%@", cityIDArray[i]];
            }
        }
        self.service = text;
        self.serviceID = idText;
        return [RACSignal empty];
    }];
}

@end
